package com.gr.dmbackend.service.Impl.student;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gr.dmbackend.mapper.StudentEventMapper;
import com.gr.dmbackend.model.StudentEvent;
import com.gr.dmbackend.service.student.MarkAsReadService;
import com.gr.dmbackend.utils.GetUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MarkAsReadServiceImpl implements MarkAsReadService {

    @Autowired
    private StudentEventMapper studentEventMapper;

    @Override
    public JSONObject markAsRead(int id) {

        JSONObject resp = new JSONObject();

        QueryWrapper<StudentEvent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", GetUserUtil.getCurrentUser().getUsername())
                .eq("event_id", id);
        StudentEvent studentEvent = studentEventMapper.selectOne(queryWrapper);
        if (studentEvent == null) {
            resp.put("error_message", "当前通知不存在");
            return resp;
        }

        studentEvent.setIsRead(true);
        studentEventMapper.updateById(studentEvent);
        resp.put("error_message", "success");

        return resp;
    }
}
